package cn.codergege.demo.tess4j

import net.sourceforge.tess4j.Tesseract
import net.sourceforge.tess4j.TesseractException
import org.junit.jupiter.api.Test
import org.springframework.boot.test.context.SpringBootTest
import java.net.URL
import javax.imageio.ImageIO

@SpringBootTest
class Tess4jDemoTest {
    @Test
    fun testCode() {
        // 获取 image
        val imageUrl = """
            http://yun.zjer.cn/imgcode.php?d=iVBORw0KGgoAAAANSUhEUgAAAGQAAAAYAgMAAABxbLB9AAAADFBMVEX%2FAAD%2F%2F%2F92l8dWAyY6FEExAAAAyElEQVQokW1SOw7DMAjNJauMuUl9pE4dW05R%2BRiePFTIphgINlGfrIT48fhmI0YnKnTFNk2UM1CM6WRXiP814VoY1EQXgGi6WDildfdoCOaoyHUwve1nBLeUocovLJQJhqVQJre7%2BPFpZH1n1xgjiZ63gw5hNHrl77xr99CSVMAadu2s%2BVT02UieV%2FI8SdWmcQaZkWGUb1oroLVqiQaxUx0DPt4QpqNSryBgWcWFWfOYm27oEZg5fJo29BCt6AN0j9uZN%2FxW4%2BIHc1k9mf%2B69qoAAAAASUVORK5CYII%3D
        """.trimIndent()
        val image = ImageIO.read(URL(imageUrl))
        // 新建 tesseract 实例
        val tesseract = Tesseract()
        tesseract.setDatapath("/usr/local/share/tessdata")
        try {
            val result = tesseract.doOCR(image)
            println("-------------------")
            println("${image.height}: $result")
            println("-------------------")
        } catch(e: TesseractException) {
            println(e.message)
        }
    }
}